// 商品通用插件操作
import Vue from 'vue'
import ProductUp from 'components/goods/productup.vue'
const ProductOperationConstructor = Vue.extend(ProductUp)
let instance
const defaultCallback = () => {

}
const initInstance = () => {
  instance = new ProductOperationConstructor({
    el: document.createElement('div')
  })
  instance.callback = defaultCallback
  document.body.appendChild(instance.$el)
}

const showProductOperation = (params = {}, type) => {
  if (!instance) {
    initInstance()
  }
  for (let key in params) {
    instance[key] = params[key]
  }
  instance.type = type
  instance.show = false
  Vue.nextTick(() => {
    instance.show = true
  })
}
const productAction = (type) => {
  return function (opts) {
    showProductOperation(opts, type)
  }
}
// Vue插件安装方法
export const install = function (Vue, opts = {}) {
  if (install.installed) return
  Vue.prototype.$productup = productAction('productup')
  Vue.prototype.$productdown = productAction('productdown')
  Vue.prototype.$productTimingUp = productAction('productTimingUp')
  Vue.prototype.$productTimingDown = productAction('productTimingDown')
  Vue.prototype.$productDelete = productAction('productDelete')
}
if (typeof window !== 'undefined' && window.Vue) {
  install(window.Vue)
}
